package com.happy.controller;


import com.happy.pojo.RequestMessage;
import com.happy.pojo.Result;
import com.happy.service.RequestMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;

/**
 * @Author: happy
 * @Date: 2021/9/27 11:52
 * @Version: 1.0
 */
@RestController
@RequestMapping("/requestMessage")
public class RequestMessageController extends BaseController {

    @Autowired
    private RequestMessageService requestMessageService;


    @RequestMapping(value = "/save", name = "保存测试案例")
    public Result saveRequestMessage(RequestMessage requestMessage) {
        Result result = new Result();
        try {
            int save = requestMessageService.save(requestMessage, user);
            if (save >= 0) {
                result.setSuccess(true);
                result.setMessage("保存成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("保存失败【" + e.getMessage() + "】");
        }

        return result;
    }

    @RequestMapping(value = "/delete", name = "删除测试案例")
    public Result deleteRequestMessage(RequestMessage requestMessage) {
        Result result = new Result();
        try {
            int delete = requestMessageService.delete(requestMessage, user);
            if (delete >= 0) {
                result.setSuccess(true);
                result.setMessage("删除成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage("删除失败【" + e.getMessage() + "】");
        }

        return result;
    }


    @RequestMapping(value = "/try", name = "调试测试案例")
    public Result tryRequestMessage(RequestMessage requestMessage) {
        Result result = new Result();
        try {
            Map repMap = requestMessageService.tryTest(requestMessage);
            Object status = repMap.get("status");
            Object responseMessage = repMap.get("responseMsg");
            if ((int) status >= 400) {
                throw new Exception(responseMessage.toString());
            }
            result.setSuccess(true);
            result.setMessage(responseMessage.toString());
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }


    @RequestMapping(value = "/test", name = "单次测试测试案例")
    public Result testRequestMessage(RequestMessage requestMessage) {
        Result result = new Result();
        try {
            String responseMessage = requestMessageService.test(requestMessage, user);
            result.setSuccess(true);
            result.setMessage(responseMessage);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMessage(e.getMessage());
        }
        return result;
    }


    @RequestMapping(value = "/tests", name = "批量测试测试案例")
    public Result testRequestMessages(RequestMessage requestMessage) {
        //todo
        return null;
    }
}
